package cn.com.generaldata.os.service.indicator_evaluation.monitoring;

import java.util.List;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.nutz.dao.sql.Criteria;
import org.nutz.lang.random.R;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.pojo.IndicatorEvaluationResult;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 综合预警指数评估指标计算类.
 */
public class ComprehensiveEarlyWarningCalculateService extends IndicatorEvaluationCalculateService {

	private final List<String> comprehensiveName = ProvinceIndicatorConstant.COMPREHEN_WARNING_NAMES;
	private final String Lighting = ProvinceIndicatorConstant.LIGHTING;
	private final String comprehenWarningName = ProvinceIndicatorConstant.COMPREHENWARNING;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {

		Double sum = 0d;
		for (String name : comprehensiveName) {
			Integer warningIndicatorId = getIndicatorEvaluationId(name, indicatorAccountVO.getAreaId());

			Double warningIndicatorVal = getWarningIndicatorVal(indicatorAccountVO, warningIndicatorId);
			String result = getResultValue(warningIndicatorId, warningIndicatorVal);
			if (result == null) {
				Double maxValue = getEvaluationMaxValue(warningIndicatorId);
				Double minValue = getEvaluationMinValue(warningIndicatorId);
				if (maxValue < warningIndicatorVal) {
					result = "5";
				} else if (minValue > warningIndicatorVal) {
					result = "1";
				} else {
					String resultMaxValue = getResultMaxValue(warningIndicatorId, warningIndicatorVal);
					String resultMinValue = getResultMinValue(warningIndicatorId, warningIndicatorVal);
					if (resultMaxValue != null) {
						if ("4".equals(resultMaxValue)) {
							result = "5";
						}
						if ("2".equals(resultMaxValue)) {
							result = "2";
						}
					}
					if (resultMinValue != null) {
						if ("2".equals(resultMinValue)) {
							result = "1";
						}
						if ("4".equals(resultMinValue)) {
							result = "4";
						}
					}
				}
			}
			Integer indicatorId = getIndicatorEvaluationId(name + Lighting, indicatorAccountVO.getAreaId());
			IndicatorEvaluationResult ier = new IndicatorEvaluationResult();
			ier.setIndicatorId(indicatorId);
			ier.setYearTime(indicatorAccountVO.getYearTime());
			ier.setIndicatorValue(Double.valueOf(result));
			ier.setTimeType(indicatorAccountVO.getTimeType());
			ier.setYear(indicatorAccountVO.getYear());
			ier.setAreaId(indicatorAccountVO.getAreaId());
			Criteria cri = getIndicatorEvaluationDataCri(ier);

			Record record = dao.fetch("tbl_indicator_evaluation_res", cri);
			if (record != null) {
				dao.clear("tbl_indicator_evaluation_res", cri);
			}
			saveEvaluationResult(ier);
			sum += Double.valueOf(result);
		}
		Integer indicatorId = getIndicatorEvaluationId(comprehenWarningName, indicatorAccountVO.getAreaId());
		String sumResult = getResultValue(indicatorId, sum);
		if (sumResult == null) {
			Double maxValue = getEvaluationMaxValue(indicatorId);
			Double minValue = getEvaluationMinValue(indicatorId);
			if (maxValue < sum) {
				sumResult = "5";
			} else if (minValue > sum) {
				sumResult = "1";
			} else {
				String resultMaxValue = getResultMaxValue(indicatorId, sum);
				String resultMinValue = getResultMinValue(indicatorId, sum);
				if (resultMaxValue != null) {
					if ("4".equals(resultMaxValue)) {
						sumResult = "5";
					}
					if ("2".equals(resultMaxValue)) {
						sumResult = "2";
					}
				}
				if (resultMinValue != null) {
					if ("2".equals(resultMinValue)) {
						sumResult = "1";
					}
					if ("4".equals(resultMinValue)) {
						sumResult = "4";
					}
				}
			}
		}
		Integer indicatorSumId = getIndicatorEvaluationId(comprehenWarningName + "增长率" + Lighting,
				indicatorAccountVO.getAreaId());
		IndicatorEvaluationResult ier = new IndicatorEvaluationResult();
		ier.setIndicatorId(indicatorSumId);
		ier.setYearTime(indicatorAccountVO.getYearTime());
		ier.setIndicatorValue(Double.valueOf(sumResult));
		ier.setTimeType(indicatorAccountVO.getTimeType());
		ier.setYear(indicatorAccountVO.getYear());
		ier.setAreaId(indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorEvaluationDataCri(ier);

		Record record = dao.fetch("tbl_indicator_evaluation_res", cri);
		if (record != null) {
			dao.clear("tbl_indicator_evaluation_res", cri);
		}
		saveEvaluationResult(ier);
		return sum;
	}

	private Criteria getIndicatorEvaluationDataCri(IndicatorEvaluationResult indicatorAccountVO) {
		Criteria cri = Cnd.cri();
		cri.where().and("indicator_id", "=", indicatorAccountVO.getIndicatorId());
		cri.where().and("time_type", "=", indicatorAccountVO.getTimeType());
		cri.where().and("year_time", "=", indicatorAccountVO.getYearTime());
		cri.where().and("area_id", "=", indicatorAccountVO.getAreaId());
		cri.where().and("year", "=", indicatorAccountVO.getYear());
		return cri;
	}

	private void saveEvaluationResult(IndicatorEvaluationResult ier) {
		// 保存新记录
		Chain chain = Chain.make("id", R.UU16());
		chain.add("indicator_id", ier.getIndicatorId());
		chain.add("area_id", ier.getAreaId());
		chain.add("time_type", ier.getTimeType());
		chain.add("year_time", ier.getYearTime());
		chain.add("year", ier.getYear());

		chain.add("indicator_value", ier.getIndicatorValue());

		dao.insert("tbl_indicator_evaluation_res", chain);
	}

	protected String getResultMaxValue(Integer warningIndicatorId, Double value) {
		String result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			String sql = "select result from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId
					+ " and value_max=" + value;
			result = sqlTemplate.queryForObject(sql, null, String.class);
		}
		return result;
	}

	protected String getResultMinValue(Integer warningIndicatorId, Double value) {
		String result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			String sql = "select result from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId
					+ " and value_min=" + value;
			result = sqlTemplate.queryForObject(sql, null, String.class);
		}
		return result;
	}

	protected String getResultValue(Integer warningIndicatorId, Double value) {
		String result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			String sql = "select result from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId
					+ " and value_min<" + value + " and value_max>" + value;
			result = sqlTemplate.queryForObject(sql, null, String.class);
		}
		return result;
	}

	protected Double getEvaluationMaxValue(Integer warningIndicatorId) {
		Double result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			String sql = "select max(value_max) from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId;
			result = sqlTemplate.queryForObject(sql, null, Double.class);
		}
		return result;
	}

	protected Double getEvaluationMinValue(Integer warningIndicatorId) {
		Double result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			// "select result from tbl_indicator_conclusion where indicator_id='306' and value_min<=0.17 and value_max>0.17";
			String sql = "select min(value_min) from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId;
			result = sqlTemplate.queryForObject(sql, null, Double.class);
		}
		return result;
	}

	private Double getWarningIndicatorVal(IndicatorAccountVO indicatorAccountVO, int id) {
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), id);
		Double result = getEvaluationIndicatorValue(cri);
		Validate.isTrue(result != null);
		return result;
	}

}
